package reader.impl;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import model.ReadResultModel;
import reader.ReaderCore;

public class XLSSourceReader implements ReaderCore {

	@SuppressWarnings("deprecation")
	@Override
	public Object readFile(String fileName, int subListType) throws Exception {

		Map<String, ArrayList<ArrayList<String>>> subListMap = new HashMap<String, ArrayList<ArrayList<String>>>();
		ArrayList<ArrayList<String>> mainList = new ArrayList<ArrayList<String>>();

		int sublistEnnteredFlag = 0;
		String recentExpandListName = null;
		ArrayList<ArrayList<String>> tempList = null;

		FileInputStream inputStream = null;
		File excelFile = new File(fileName);
		inputStream = new FileInputStream(excelFile);
		Workbook workbook = null;
		String fileType = fileName.substring(fileName.lastIndexOf('.'));
		switch (fileType) {
		case ".xls":
			workbook = new HSSFWorkbook(inputStream);
			break;
		case ".xlsx":
			workbook = new XSSFWorkbook(inputStream);
			break;
		default:
			break;
		}

		Sheet sheet = workbook.getSheetAt(0);
		int rowEnd = sheet.getPhysicalNumberOfRows();
		for (int rowNum = sheet.getFirstRowNum(); rowNum < rowEnd; rowNum++) {
			Row row = sheet.getRow(rowNum);
			
			int fullNum = row.getLastCellNum();
			//合并的单元格的后几个格子是空值，但会被计算到lastcellnum上
			int emptyNum = 6 - fullNum;
			for (int i = 0; i < fullNum; i++) {
				row.getCell(i).setCellType(CellType.STRING);
				if (row.getCell(i).getStringCellValue().isEmpty()) {
					emptyNum ++;
				}
			}
			
			switch (emptyNum) {
			case 0:
			case 1:
				ArrayList<String> mainlistRecord = new ArrayList<String>();
				for (int i = 0; i < 6; i++) {
					row.getCell(i).setCellType(CellType.STRING);
					mainlistRecord.add(row.getCell(i).getStringCellValue());
				}
				mainList.add(mainlistRecord);
				break;
			case 2:
			case 3:
				if (sublistEnnteredFlag == 1) {
					tempList = new ArrayList<>();
					sublistEnnteredFlag = 0;
				}
				ArrayList<String> sublistRecord = new ArrayList<String>();
				if (0 == subListType) {
					for (int i = 0; i < 3; i++) {
						row.getCell(i).setCellType(CellType.STRING);
						sublistRecord.add(row.getCell(i).getStringCellValue());
					}
				} else {
					for (int i = 0; i < 4; i++) {
						row.getCell(i).setCellType(CellType.STRING);
						sublistRecord.add(row.getCell(i).getStringCellValue());
					}
				}
				if (tempList != null)
					tempList.add(sublistRecord);
				break;
			case 5:
				sublistEnnteredFlag = 1;
				if (tempList != null && !tempList.isEmpty()) {
					subListMap.put(recentExpandListName, tempList);
				}
				// 填充附表map的键值
				StringBuilder sBuilder = new StringBuilder();
				String readyToParse = row.getCell(0).getStringCellValue();
				if (readyToParse != null && readyToParse.length() > 0) {
					for (int i = 0; i < readyToParse.length(); i++) {
						if (readyToParse.charAt(i) >= 65 && readyToParse.charAt(i) <= 122) {
							sBuilder.append(readyToParse.charAt(i));
						} else {
							break;
						}
					}
				}
				recentExpandListName = sBuilder.toString();
				break;
			default:
				break;
			}
		}
		// 为最后一张附表触发添加
		if (tempList != null && !tempList.isEmpty()) {
			subListMap.put(recentExpandListName, tempList);
		}

		ReadResultModel resultModel = new ReadResultModel();	
		resultModel.setMainList(mainList);
		resultModel.setSubListMap(subListMap);		
		inputStream.close();
		return resultModel;
	}
}
